'use strict';

/* Controllers */

angular.module('myApp.controllersGame', [])
        .controller('PlayOwnerCtrl', ['$scope', '$location',
            'serviceFirebaseUsers', 'serviceFirebaseGames',
            'serviceGame', 'serviceUser',
            function($scope, location, firebaseUser, firebaseGame, game, user) {
                /**
                 * start after content id loaded
                 */
                $scope.$on('$viewContentLoaded', function() {

                    game.ownerUser = user.getData();
                    game.value = 1;
                    
                    game.createGame(
                            
                        // html elements for canvas
                        'tictactoeCanvas',
                        'tictactoeContainer',
                        
                        // callback when your oponent will leave the game
                        function() {
                            $('#popupOponentLeft').modal('show');
                            $('#popupOponentLeft').on('hidden.bs.modal', function(e) {
                                $('#popupOponentLeft').off('hidden.bs.modal');
                                console.log('modal hidden');
                                location.path('/game/players');
                                $scope.$apply();
                            });
                        },
                        
                        // callback if you invite someone to game
                        function() {
                            $('#popupWaitEmail').html(game.playerUserTmp.email);
                            $('#popupWait').modal('show');
                        }
                    );

                });
            }
        ])
        .controller('PlayJoinCtrl', ['$scope', '$location',
            'serviceFirebaseGames', 'serviceGame', 'serviceUser',
            function($scope, location, firebaseGame, game, user) {
                
                /**
                 * start after content id loaded
                 */
                $scope.$on('$viewContentLoaded', function() {

                    if (game.ownerUser) {

                        game.playerUser = user.getData();
                        game.value = 2;
                        game.joinGame(

                            // html elements for canvas
                            'tictactoeCanvas',
                            'tictactoeContainer',

                            // callback when your oponent will leave the game
                            function() {
                                $('#popupOponentLeft').modal('show');
                                $('#popupOponentLeft').on('hidden.bs.modal', function(e) {
                                    $('#popupOponentLeft').off('hidden.bs.modal');
                                    console.log('modal hidden');
                                    location.path('/game/players');
                                    $scope.$apply();
                                });
                            }
                        );

                    }
                });
            }
        ])
        .controller('QuitCtrl', ['$scope', '$location', 'serviceGame', 'serviceUser', 'serviceFirebaseUsers',
            function($scope, location, game, user, firebaseUsers) {
                /**
                 * you have left the game so it is your fault
                 */
                game.setLoser(user, function () {
                    game.quit(function() {
                        location.path('/game/players');
                        $scope.$apply();
                    });
                });

            }
        ]);